package com.pswv2.backstage.share.keyword.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.backstage.share.keyword.service.ShareKWordService;
import com.pswv2.bub.bsc.dao.po.ShareKWord;
import com.pswv2.bub.bsc.dao.po.UserShareKWord;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.Paging;
import com.pswv2.util.Strings;

@Controller
@RequestMapping(value = "shareKWordAction", produces = { "text/json;charset=UTF-8" })
public class ShareKWordAction {
	private static Class<?> poc = ShareKWord.class;
	private static Class<?> poc1 = UserShareKWord.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private ShareKWordService shareKWordService;
	
	/**
	 * 	pc后台选中通过
	 */
	@ResponseBody
	@RequestMapping(value={"pitchOnPass"})
	public String pitchOnPass(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			ShareKWord entity = shareKWordService.queryById(a);
			if(entity.getStatus()==entity.status_pass||entity.getStatus()==entity.status_reject){
				return CommonResp.buildErrorResp(BackendErrorCode.already_auditing).toJsonString();
			}
		}
		for(int a:items){
			ShareKWord entity = shareKWordService.queryById(a);
			entity.setStatus(entity.status_pass);
			extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 	pc后台选中驳回
	 */
	@ResponseBody
	@RequestMapping(value={"pitchOnReject"})
	public String pitchOnReject(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			ShareKWord entity = shareKWordService.queryById(a);
			if(entity.getStatus()==entity.status_pass||entity.getStatus()==entity.status_reject){
				return CommonResp.buildErrorResp(BackendErrorCode.already_auditing).toJsonString();
			}
		}
		for(int a:items){
			ShareKWord entity = shareKWordService.queryById(a);
			entity.setStatus(entity.status_reject);
			extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 	pc后台删除选中的
	 */
	@ResponseBody
	@RequestMapping(value={"pitchOnDelete"})
	public String pitchOnDelete(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			ShareKWord entity = shareKWordService.queryById(a);
			extraSpringHibernateTemplate.getHibernateTemplate().delete(entity);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app查看用户拥有自己的shareKWord
	 */
	@ResponseBody
	@RequestMapping(value = { "queryUserMyShareKWord" })
	public String queryUserMyShareKWord(Integer userId) {
		Criterion criterion = Restrictions.eq("userId", userId);
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("shareKWordId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	/**
	 * app保存用户拥有的shareKWord
	 */
	@ResponseBody
	@RequestMapping(value = { "saveUserShareKWord" })
	public String saveUserShareKWord(Integer userId,Integer shareKWordId) {
		UserShareKWord usk=new UserShareKWord();
		usk.setShareKWordId(shareKWordId);
		usk.setUserId(userId);
		extraSpringHibernateTemplate.getHibernateTemplate().save(usk);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app查询用户拥有的shareKWord
	 */
	@ResponseBody
	@RequestMapping(value = { "queryUserShareKWord" })
	public String queryUserShareKWord(Integer userId) {
		Criterion criterion = Restrictions.eq("userId", userId);
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc1, Order.desc("userShareKWordId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc1, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	/**
	 * 后台审核通过
	 */
	@ResponseBody
	@RequestMapping(value = { "platformAdopt" })
	public String platformAdopt(Integer shareKWordId) {
		ShareKWord shareKWord = shareKWordService.queryById(shareKWordId);
		shareKWord.setStatus(1);
		extraSpringHibernateTemplate.getHibernateTemplate().update(shareKWord);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 后台审核未通过
	 */
	@ResponseBody
	@RequestMapping(value = { "platformNotAdopt" })
	public String platformNotAdopt(Integer shareKWordId) {
		ShareKWord shareKWord = shareKWordService.queryById(shareKWordId);
		shareKWord.setStatus(2);
		extraSpringHibernateTemplate.getHibernateTemplate().update(shareKWord);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app添加
	 */
	@ResponseBody
	@RequestMapping(value = { "androidadd" })
	public String androidadd(String kwords, String content,Integer userId) {
		List<ShareKWord> allkowrd1 =(List<ShareKWord>)extraSpringHibernateTemplate.getHibernateTemplate().find("From ShareKWord where kwords=? and content=? and userId=? ", kwords,content,userId);
		if(allkowrd1.size()!=0){
			return CommonResp.buildErrorResp(BackendErrorCode.add_error).toJsonString();
		}
		ShareKWord shareKWord = new ShareKWord();
		Long s=new Date().getTime();
		shareKWord.setReleaseTime(s);
		shareKWord.setKWords(kwords);
		shareKWord.setContent(content);
		shareKWord.setUserId(userId);
		shareKWord.setStatus(ShareKWord.status_wait);
		shareKWordService.add(shareKWord);
		return CommonResp.buildSuccessResp(shareKWord).toJsonString();
	}
	/**
	 * 添加
	 */
	@ResponseBody
	@RequestMapping(value = { "add" })
	public String add(String kwords, String content, Integer userId, HttpSession session) {
		ShareKWord shareKWord = new ShareKWord();
		shareKWord.setKWords(kwords);
		shareKWord.setContent(content);
		shareKWord.setUserId(userId);
		shareKWordService.add(shareKWord);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	/**
	 * 查看所有
	 */
	@ResponseBody
	@RequestMapping(value = { "listAll" })
	public String list(HttpSession session) {
		List<ShareKWord> list=shareKWordService.queryAll();
		/*Criterion criterion = Restrictions.eq("status", ShareKWord.status_pass);
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("shareKWordId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);*/
		return CommonResp.buildSuccessResp(list).toJsonString();
	}
	
	/**
	 * 查看所有分页
	 */
	@ResponseBody
	@RequestMapping(value = { "list" })
	public String list(int pageIndex, int pageSize, HttpSession session) {
		pageIndex=pageIndex*pageSize;
		Paging<?> paging = extraSpringHibernateTemplate.findPage(poc, Order.desc("shareKWordId"),pageIndex, pageSize);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	/**
	 * 通过Id查询
	 */
	
	@ResponseBody
	@RequestMapping(value = { "queryById" })
	public String queryById(int id) {
		ShareKWord shareKWord = shareKWordService.queryById(id);
		if (shareKWord == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		return CommonResp.buildSuccessResp(shareKWord).toJsonString();
	}

	/**
	 * 修改
	 */
		
	@ResponseBody
	@RequestMapping(value = { "edit" })
	public String edit(Integer id, String kwords, String content, HttpSession session) {
		ShareKWord toUpdate = shareKWordService.queryById(id);
		if (toUpdate == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
	if (StringUtils.isNoneBlank(kwords)) {
			toUpdate.setKWords(Strings.trimAndNotNull(kwords));
		}
		if (StringUtils.isNoneBlank(content)) {
			toUpdate.setContent(Strings.trimAndNotNull(content));
		}
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
	
	/**
	 * 条件查询
	 */

	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String kwords, HttpSession session) throws ParseException {
		
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(kwords)) {
			criterions.add(Restrictions.ilike("kwords", "%" + kwords + "%"));
		}
		//criterions.add(Restrictions.eq("status", ShareKWord.status_pass));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("shareKWordId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	/**
	 * 删除
	 */
	
	@ResponseBody
	@RequestMapping(value = { "remove" })
	public String remove(int id) {
		ShareKWord kWord = shareKWordService.queryById(id);
		if (kWord == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(kWord);
		return CommonResp.buildSuccessResp().toJsonString();
	}
		
	
}
